const {dialog} = require('electron')
const {app} = require('electron')
function formatLocalDate(date) {
    const y = date.getFullYear();
    const m = String(date.getMonth() + 1).padStart(2, '0');
    const d = String(date.getDate()).padStart(2, '0');
    const h = String(date.getHours()).padStart(2, '0');
    const min = String(date.getMinutes()).padStart(2, '0');
    const s = String(date.getSeconds()).padStart(2, '0');
    return `${y}-${m}-${d} ${h}:${min}:${s}`;
}

// 验证日期格式 - 方法一(粗略)
function isValidDateTimeFormat(dateTimeStr) {
    const regex = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
    return regex.test(dateTimeStr);
}

// 验证日期格式 - 方法二(严格)
function isValidDateTime(dateTimeStr) {
    const date = new Date(dateTimeStr);
    // 检查转换后的日期是否有效以及是否与输入完全匹配
    return !isNaN(date.getTime()) && this.formatLocalDate(date) === dateTimeStr;
}

async function showErrorAndQuit(message) {
    await dialog.showMessageBox({
        type: 'error',
        title: '提示',
        message: message,
    });
    app.quit();
}

module.exports = {
    formatLocalDate,
    isValidDateTimeFormat,
    isValidDateTime,
    showErrorAndQuit
};
